package com.bj58.mis.apm.server.statistics.suggest.parameterize;

import com.bj58.mis.apm.server.core.util.TimesUtil;
import com.bj58.mis.apm.server.core.util.UnitUtil;
import com.bj58.mis.apm.server.core.util.UnitValue;

import java.util.ArrayList;
import java.util.Set;

/**
 * @Author: wangxw
 * @Date: 2018/10/20 17:33
 */
public class AnalysisUtil {

    /**
     * 解析模板，需要将内容填充
     */
    private final static String ANALYSIS_ROWKEY1 = "Cube定义包含#v1个维度，查询中使用了#v2个维度。";
    private final static String ANALYSIS_ROWKEY2 = "未用到的维度为：#v3。";
    private final static String ANALYSIS_SHRINK = "cuboid共#v1个，相对重合率大于95%的有#v2个，等于100%的有#v3个，相关维度为#v4。";
    private final static String ANALYSIS_SWELL1 = "膨胀率为#v1，结果占用#v2存储。";
    private final static String ANALYSIS_SWELL2 = "未达到膨胀率预警值#v3，调优建议对结果影响较小。";
    private final static String ANALYSIS_SWELL3 = "已达到膨胀率预警值#v3，可根据调优建议进行操作。";
    private final static String ANALYSIS_QUERY1 = "由于查询次数为#v1次，通过衰减值#v2计算得出有效查询次数为#v3，";
    private final static String ANALYSIS_QUERY2 = "调优建议对结果影响较小。";
    private final static String ANALYSIS_QUERY3 = "可根据实际使用需求和调优建议进行优化。";
    private final static String ANALYSIS_SEGMENT1 = "Cube构建结果包括#v1个分区。";
    private final static String ANALYSIS_SEGMENT2 = "分区较多，会影响跨分区的查询效率。";
    private final static String ANALYSIS_BUILD1 = "Cube#v1构建耗时#v2。";
    private final static String ANALYSIS_BUILD2 = "属于较慢构建。";

    /**
     * 维度与查询情况分析
     * @param total
     * @param used
     * @return
     */
    public static String getAnalysisRowkey(int total, int used, Set<String> unUsedRk){
        return ANALYSIS_ROWKEY1.replace("#v1", total+"").replace("#v2", used+"") + (unUsedRk.size() > 0 ? ANALYSIS_ROWKEY2.replace("#v3", ProposalsUtil.getString(new ArrayList<>(unUsedRk))) : "");
    }

    /**
     * cuboid重合率情况分析
     * @param total
     * @param abs95
     * @param abs100
     * @param rklist
     * @return
     */
    public static String getAnalysisShrink(int total, int abs95, int abs100, String rklist){
        return ANALYSIS_SHRINK.replace("#v1", total+"").replace("#v2", abs95+"").replace("#v3", abs100+"").replace("#v4", rklist);
    }

    /**
     * 读取膨胀倍数情况分析
     * @param swell
     * @param storage
     * @param warn
     * @return
     */
    public static String getAnalysisSwell(double swell, long storage, double warn){
        UnitValue uv = UnitUtil.deal(storage);
        return ANALYSIS_SWELL1.replace("#v1", swell + "").replace("#v2", uv.getValue() + uv.getUint())
                + (swell > warn ? ANALYSIS_SWELL3.replace("#v3", warn+""):ANALYSIS_SWELL2.replace("#v3", warn+""));
    }

    /**
     * 读取查询分析
     * @param queries
     * @param warn
     * @return
     */
    public static String getAnalysisQuery(int queries, int effective, int warn, double damping){
        return ANALYSIS_QUERY1.replace("#v1", queries+"").replace("#v2", damping+"").replace("#v3", effective+"")
                + (effective < warn ? ANALYSIS_QUERY2 : ANALYSIS_QUERY3);
    }

    /**
     * 分区情况分析
     * @param segment
     * @param warn
     * @return
     */
    public static String getAnalysisSegment(int segment, int warn){
        return ANALYSIS_SEGMENT1.replace("#v1", segment+"") + (segment > warn ? ANALYSIS_SEGMENT2 : "");
    }

    /**
     * 构建情况分析
     * @param type
     * @param duration
     * @param warn
     * @return
     */
    public static String getAnalysisBuild(String type, long duration, long warn) {
        return ANALYSIS_BUILD1.replace("#v1", type).replace("#v2", TimesUtil.formatTimes(duration)) + (duration > warn ? ANALYSIS_BUILD2 : "");
    }
}
